<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\business\BusinessAdress;
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessAdress */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="business-adress-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>

                        <?= $form->field($model, 'type')->dropDownList($types) ?>

                        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'parentid')->dropDownList(ArrayHelper::map(BusinessAdress::allAddress(),'id','name')) ?>

                        <div class="form-group field-businessadress-parentid required has-success">
                            <div class="col-xs-3 col-sm-2 text-right">
                                <label class="control-label" for="businessadress-parentid">上级</label>
                            </div>
                            <div class="col-xs-9 col-sm-7">
                                    <div id="l-map" style="height: 300px;width: 100%">

                                    </div>
                                    <div id="r-result">
                                        请输入地址进行检索:<input class="form-control" type="text" id="suggestId" size="20" value="百度" style="width:150px;" />
                                    </div>
                                    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;">

                                    </div>
                                    <div class="col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0">
                                        <div class="help-block">

                                        </div>
                                    </div>
                            </div>
                        </div>


                        <?= $form->field($model, 'lat')->textInput() ?>

                        <?= $form->field($model, 'lng')->textInput() ?>

                        <?= $form->field($model, 'status')->dropDownList(BusinessAdress::$statues) ?>

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                    </div>
                    <input type="hidden" id="_csrf" name="_csrf" value="<?php Yii::$app->request->csrfToken ?>">
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>


<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Y97ouPtBnpfA5wewLUxT4m4G9AkNQRbK"></script>
<script>
    <?php $this->beginBlock('js_end') ?>

    $('#businessadress-type').change(function () {
        $.get('/business/address/plist',{'type':$(this).val(),'_csrf':$('#_csrf').val()},function(res){
            console.log(res);
            var html = '';
            $.each(res,function(index){
                html += "<option value="+res[index].id+">"+res[index].name+"</option>"
            });
            $('#businessadress-parentid').html(html);
        },'json')
    });



        // 百度地图API功能
        function G(id) {
            return document.getElementById(id);
        }

        var map = new BMap.Map("l-map");
        map.centerAndZoom("柞水",12);                   // 初始化地图,设置城市和地图级别。

        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {"input" : "suggestId"
                ,"location" : map
            });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });


        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 18);
                map.addOverlay(new BMap.Marker(pp));    //添加标注

                //添加到经纬度输入框
                $('#businessadress-lat').val(pp.lat);
                $('#businessadress-lng').val(pp.lng);

            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }



    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>
